import random
from random import randint

WEST = 0
NORTH = 1
EAST =2
SOUTH = 3

directions = [[-1,0], #west
              [0,1],  #north
              [1,0],  #east
              [0,-1]] #south

class room:
    def __init__(self,x,y,w,h):
        self.x = x
        self.y = y
        self.w = w
        self.h = h

class coridor:
    def __init__(self,x,y,x1,y1):
        self.x = x
        self.y = y
        self.x1 = x1
        self.y1 = y1
        
class dang:
    
    def __init__(self):
        self.coridors = []
        self.rooms = []
        self.minLength = 10
        self.maxLength = 60
        self.roomW = 10
        self.roomH = 15
        
    def addRoom(self,der,x,y,invert=1):
        r = room(0,0,random.randint(5,self.roomW),random.randint(5,self.roomH))
        s = random.random()
        if directions[der][0] == 0: 
            r.x=x-r.w*s
            r.y=y-r.h*(1+directions[der][1]*invert)/2
        else:
            r.x=x-r.w*(1+directions[der][0]*invert)/2
            r.y=y-r.h*s
        self.rooms.append(r)

    def generateLab(self,startX,startY):
        x = startX
        y = startY
        startDer = der = random.choice([WEST,NORTH,EAST,SOUTH])
        length = random.randint(self.minLength,self.maxLength)
        i=1
        coridor = []
        coridor.append((x,y))
        while i<=length:
            size = randint(1,10)
            x+=directions[der][0]*size
            y+=directions[der][1]*size
            coridor.append((x,y))
            tempDer=der
            der = der + random.randint(-1,1) 
            if der<0:
                der = 3
            elif der>3:
                der = 0
            i+=size
        self.addRoom(startDer, startX, startY)
        self.addRoom(tempDer, coridor[-1][0], coridor[-1][1],-1)
        return coridor
            
    def generate(self,startX,startY):
        self.coridors.append(self.generateLab(startX, startY))
        for i in range(randint(1,4)):
            s = random.choice(self.coridors[-1])
            self.coridors.append(self.generateLab(s[0], s[1]))
            
            
class lab:
    """
    create a set of the rooms
    """
    def __init__(self,w,h,c,mins,maxs):
        self.w = w
        self.h = h
        self.roomcount = c
        self.minsize = mins
        self.rooms = []
        self.maxsize = maxs

    def clear(self):
        del self.rooms[:]

    def generate(self,c=None):
        if c!=None:
            self.roomcount=c
        i=0
        while i<self.roomcount:
            x = random.randint(0,self.w-self.maxsize)
            y = random.randint(0,self.h-self.maxsize)
            w = random.randint(self.minsize,self.maxsize)
            h = random.randint(self.minsize,self.maxsize)
            self.rooms.append(room(x,y,w,h))
            i+=1


if __name__ == "__main__":
    a=lab(10,10,2,2)
    a.generate(4)
    print a.rooms
